package hczc.longcai.com.hccrowdfunding.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.home.first_index.beans.Banner;
import hczc.longcai.com.hccrowdfunding.home.first_index.beans.Information;
import hczc.longcai.com.hccrowdfunding.home.first_index.beans.ProjectInfo;

/**
 * 首页数据库操作类
 * Created by Administrator on 2016/2/24.
 */
public class DBIndexController {

    private static final String TAG = "DBIndexController";
    private Context context;

    private final DBHelper helper;
    private final SQLiteDatabase db;

    private ContentValues contentValues;


    public DBIndexController(Context context) {
        this.context = context;
        helper = new DBHelper(this.context);
        db = helper.getReadableDatabase();
    }

    /**
     * 清空Banner表
     */
    public void clearBanners() {
        String clearBanner = "DELETE FROM " + DBHelper.Banner.TABLE_BANNER;
        String setZero = "UPDATE sqlite_sequence SET seq = 0 WHERE name = '" + DBHelper.Banner.TABLE_BANNER + "'";
        db.execSQL(clearBanner);
        db.execSQL(setZero);
    }

    /**
     * 清空资讯表
     */
    public void clearInformation() {
        String clearInformation = "DELETE FROM " + DBHelper.Information.TABLE_INFORMATION;
        String setZero = "UPDATE sqlite_sequence SET seq = 0 WHERE name = '" + DBHelper.Information.TABLE_INFORMATION + "'";
        db.execSQL(clearInformation);
        db.execSQL(setZero);
    }

    /**
     * 清空项目项目表
     */
    public void clearProject() {
        String clearProject = "DELETE FROM " + DBHelper.Project.TABLE_PROJECT;
        String setZero = "UPDATE sqlite_sequence SET seq = 0 WHERE name = '" + DBHelper.Project.TABLE_PROJECT + "'";
        db.execSQL(clearProject);
        db.execSQL(setZero);
    }


    /**
     * 添加资讯
     *
     * @param list 存放资讯的集合
     */
    public void addInformation(List<Information> list) {
        clearInformation();
        List<Information> informations = null;
        if (list != null) {
            informations = list;
            int size = informations.size();
            if (contentValues == null) {
                contentValues = new ContentValues();
            }
            for (int i = 0; i < size; i++) {
                contentValues.putNull(DBHelper.Information._ID);
                contentValues.put(DBHelper.Information.COLUMN_NAME_INFORMATION_ID, list.get(i).getId());
                contentValues.put(DBHelper.Information.COLUMN_NAME_INFORMATION_TITLE, list.get(i).getTitle());
                long index = db.insert(DBHelper.Information.TABLE_INFORMATION, null, contentValues);
                Log.d(TAG, "插入了第" + index + "条资讯");
            }
            contentValues.clear();

        }
    }

    public List<ProjectInfo> getProinfo() {
        List<ProjectInfo> list = null;
        Cursor cursor = db.query(DBHelper.Project.TABLE_PROJECT, null, null, null, null, null, null, null);
        if (cursor != null) {
            list = new ArrayList<>();
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_ID));
                String type = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_TYPE));
                String name = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_NAME));
                String money = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_MONEY));
                String hasBorrow = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_HAS_BORROW));
                String times = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_BORROW_TIMES));
                String img = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_BORROW_IMG));
                String duration = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_BORROW_DURATION));
                String rate = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_RATE));
                String progress = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_PROGRESS));
                String info = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROJECT_INFO));
                String status = cursor.getString(cursor.getColumnIndex(DBHelper.Project.COLUMN_NAME_PROFECT_STATUS));
                ProjectInfo projectInfo = new ProjectInfo(id, type, name, money, hasBorrow, times, img, duration, rate, progress, info, status);
                list.add(projectInfo);
            }
            cursor.close();

        }

        return list;
    }

    /**
     * 添加项目信息
     *
     * @param list 获取到的信息集合
     */
    public void addProinfo(List<ProjectInfo> list) {
        clearProject();
        List<ProjectInfo> projectInfos = null;
        if (list != null) {
            projectInfos = list;
            int size = projectInfos.size();
            if (contentValues == null) {
                contentValues = new ContentValues();
            }
            for (int i = 0; i < size; i++) {
                contentValues.putNull(DBHelper.Project._ID);
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_ID, list.get(i).getId());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_TYPE, list.get(i).getType());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_NAME, list.get(i).getName());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_MONEY, list.get(i).getMoney());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_HAS_BORROW, list.get(i).getHas_borrow());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_BORROW_TIMES, list.get(i).getTime());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_BORROW_IMG, list.get(i).getImg());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_BORROW_DURATION, list.get(i).getDuration());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_RATE, list.get(i).getRate());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_PROGRESS, list.get(i).getProgress());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROJECT_INFO, list.get(i).getInfo());
                contentValues.put(DBHelper.Project.COLUMN_NAME_PROFECT_STATUS, list.get(i).getStatus());
                long insert = db.insert(DBHelper.Project.TABLE_PROJECT, null, contentValues);
                Log.d(TAG, "插入了第" + insert + "条数据");
            }
            contentValues.clear();
        }
    }

    /**
     * 添加banner
     *
     * @param list 存放banner的集合
     */
    public void addBanners(List<Banner> list) {
        clearBanners();
        List<Banner> banners = null;
        if (list != null) {
            banners = list;
            int size = banners.size();
            if (contentValues == null) {
                contentValues = new ContentValues();
            }
            for (int i = 0; i < size; i++) {
                contentValues.putNull("_id");
                contentValues.put(DBHelper.Banner.COLUMN_NAME_BANNER_IMG, list.get(i).getPic());
                contentValues.put(DBHelper.Banner.COLUMN_NAME_BANNER_URL, list.get(i).getUrl());
                long index = db.insert(DBHelper.Banner.TABLE_BANNER, null, contentValues);
                Log.d(TAG, "插入了第" + index + "条Banner");
            }
            contentValues.clear();
        }

    }


    /**
     * 查询资讯
     */
    public List<Information> findInformations() {
        List<Information> list = null;
        Cursor cursor = db.query(DBHelper.Information.TABLE_INFORMATION, null, null, null, null, null, null);
        if (cursor != null) {
            list = new ArrayList<>();
            Information information = null;
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(DBHelper.Information.COLUMN_NAME_INFORMATION_ID));
                String title = cursor.getString(cursor.getColumnIndex(DBHelper.Information.COLUMN_NAME_INFORMATION_TITLE));
                information = new Information(id, title);
                list.add(information);
            }
            cursor.close();
        }
        return list;
    }

    /**
     * 查询banner
     */
    public List<Banner> findBanners() {
        List<Banner> list = null;
        Cursor cursor = db.query(DBHelper.Banner.TABLE_BANNER, null, null, null, null, null, null);
        if (cursor != null) {
            list = new ArrayList<>();
            Banner banner = null;
            while (cursor.moveToNext()) {
                String pic = cursor.getString(cursor.getColumnIndex(DBHelper.Banner.COLUMN_NAME_BANNER_IMG));
                String url = cursor.getString(cursor.getColumnIndex(DBHelper.Banner.COLUMN_NAME_BANNER_URL));
                banner = new Banner(pic, url);
                list.add(banner);
            }
            cursor.close();
        }
        return list;
    }

    public void closeDB() {
        db.close();
    }


}
